<?php

namespace App\Models;



use Illuminate\Database\Eloquent\Relations\BelongsToMany;

use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use DateTimeInterface;

/**
 * App\Models\Users
 *
 * @property int $id
 * @property string $name
 * @property string $password
 * @property string $nickname
 * @property string $name_py_f
 * @property string $name_py_s
 * @property string $email
 * @property int $phone
 * @property string $avator
 * @property int $department_id
 * @property int $depart_leader
 * @property boolean $is_supper_admin
 * @property boolean $is_admin
 * @property boolean $is_locked
 * @property boolean $status
 * @property string $binding
 * @property boolean $ding_status
 * @property string $ding_user_id
 * @property string $ding_nickname
 * @property int $ding_phone
 * @property string $updated_at
 * @property string $created_at
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection<int, \Illuminate\Notifications\DatabaseNotification> $notifications
 * @property-read int|null $notifications_count
 * @method static \Illuminate\Database\Eloquent\Builder|Users newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Users newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Users query()
 * @method static \Illuminate\Database\Eloquent\Builder|Users whereAvator($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users whereBinding($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users whereDepartmentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users whereDingNickname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users whereDingPhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users whereDingStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users whereDingUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users whereIsAdmin($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users whereIsSupperAdmin($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users whereNamePyF($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users whereNamePyS($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users whereNickname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Users whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Users extends Authenticatable implements JWTSubject
{
    use  Notifiable;

    /**
     * @var array
     */
    protected $fillable = [
        'name',
        'password',
        'nickname',
        'name_py_f',
        'name_py_s',
        'email',
        'phone',
        'avator',

        'is_supper_admin',
        'is_admin',
        'status',
        'is_locked',
        'last_login_at',
        'last_login_ip',
        'token',
        'binding',
        'ding_status',
        'ding_user_id',
        'ding_nickname',
        'ding_phone',
        'is_first_login',
        'updated_at',
        'created_at'
    ];

    public $timestamps = false;

    public function serializeDate(DateTimeInterface $date): string
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function getJWTIdentifier()
    {
        return $this->getKey();
        // TODO: Implement getJWTIdentifier() method.
    }

    public function getJWTCustomClaims()
    {
        return ['role',"webApp"];
        // TODO: Implement getJWTCustomClaims() method.
    }

    protected $hidden = ['password'];

    /**
     * 用户的所有角色及其权限
     * @return BelongsToMany
     */
    public function roles(): BelongsToMany
    {
        return $this->belongsToMany('App\Models\Role', "user_role", "user_id", "role_id");
    }
    public function department(): \Illuminate\Database\Eloquent\Relations\HasOne
    {
        return $this->hasOne('App\Models\Department', "id", "department_id");
    }

    /**
     * 用户额外追加的权限
     * @return BelongsToMany
     */
    public function extraPermissions(){
        return $this->belongsToMany('App\Models\Permission', 'user_extra_permission','user_id', 'permission_id');
    }

}
